﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using log4net;
using CashFlow.Db.DAL;
using CashFlow.Db.Entity;

using SysTool._00_Def;

namespace SysTool._02_Service
{
    public class SecondmentService
    {
        private static ILog _logger = LogManager.GetLogger(typeof(SecondmentService));

        public static bool Add(int fromShopManagerUserId, int toShopManagerUserId, IList<int> userIdList)
        {
            using (var db = SqlSugarHelper.GetDb())
            {
                string date = DateTime.Now.ToString("yyyy-MM-dd");
                var ls = db.Queryable<SecondmentEntity>()
                    .Where(x => x.CreateTime.ToString().Substring(0, 10) == date
                        && x.FromShopManagerUserId == fromShopManagerUserId
                        && x.ToShopManagerUserId == toShopManagerUserId)
                    .Select(x => x.UserId)
                    .ToList();
                var ls2 = userIdList.Where(x => !ls.Contains(x))
                    .Select(x => new SecondmentEntity
                    {
                        UserId = x,
                        CreateTime = DateTime.Now,
                        FromShopManagerUserId = fromShopManagerUserId,
                        ToShopManagerUserId = toShopManagerUserId
                    }).ToList();
                int n = db.Insertable<SecondmentEntity>(ls2).ExecuteCommand();
                return n > 0;
            }
        }

        public static IList<MySecondmentInfo> QueryByFromShopManagerUserId(int fromShopManagerUserId, string date)
        {
            using (var db = SqlSugarHelper.GetDb())
            {
                var ls = db.Queryable<SecondmentEntity>()
                    .InnerJoin<ShopInfoEntity>((s, si1) => s.FromShopManagerUserId == si1.ShopManagerUserId)
                    .InnerJoin<ShopInfoEntity>((s, si1, si2) => s.ToShopManagerUserId == si2.ShopManagerUserId)
                    .InnerJoin<UserInfoEntity>((s, si1, si2, u) => s.UserId == u.Id)
                    .Where((s, si1, si2, u) => s.CreateTime.ToString().Substring(0, 10) == date
                        && s.FromShopManagerUserId == fromShopManagerUserId)
                    .Select((s, si1, si2, u) => new MySecondmentInfo
                    {
                        SecondmentInfo = s,
                        FromShopInfo = si1,
                        ToShopInfo = si2,
                        UserInfo = u
                    })
                    .ToList();
                return ls;
            }
        }

        public static IList<MySecondmentInfo> QueryByRecvShopManagerUserId(int recvShopManagerUserId, string date)
        {
            using (var db = SqlSugarHelper.GetDb())
            {
                var ls = db.Queryable<SecondmentEntity>()
                    .InnerJoin<ShopInfoEntity>((s, si1) => s.FromShopManagerUserId == si1.ShopManagerUserId)
                    .InnerJoin<ShopInfoEntity>((s, si1, si2) => s.ToShopManagerUserId == si2.ShopManagerUserId)
                    .InnerJoin<UserInfoEntity>((s, si1, si2, u) => s.UserId == u.Id)
                    .Where((s, si1, si2, u) => s.CreateTime.ToString().Substring(0, 10) == date
                        && s.ToShopManagerUserId == recvShopManagerUserId)
                    .Select((s, si1, si2, u) => new MySecondmentInfo
                    {
                        SecondmentInfo = s,
                        FromShopInfo = si1,
                        ToShopInfo = si2,
                        UserInfo = u
                    })
                    .ToList();
                return ls;
            }
        }
    }
}
